//---------------------------------------------------------------------------------
// Copyright (c) 2008 VoiceLynx Project
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not be
//        misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//        distribution.
//---------------------------------------------------------------------------------

#include "VLPrerequisites.h"

#include "VLStandAlone.h"



int WINAPI WinMain(HINSTANCE, HINSTANCE, char* argv, int argc)
{

  #ifdef _DEBUG
    //tell me in Debug Output when I leak memory
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    //_CrtSetBreakAlloc(198);
  #endif


  //#ifdef _DEBUG
  //turn the console on (since we are using Windows subsystem)
  AllocConsole();
  FILE* f;
  freopen_s(&f, "conout$", "w", stdout);
  freopen_s(&f, "conout$", "w", stderr);
  freopen_s(&f, "conin$", "r", stdin);
  //#endif

  VLStandaloneServer server;
  server.beginListening();

  //code based on Dean Harding's service wrapper
  //http://www.gamedev.net/reference/articles/article1899.asp

  HANDLE quitEvent;

  //for some reason I can't get it to work with just CreateEvent.. will investigate 
  //later
  quitEvent = CreateEventA( NULL, FALSE, FALSE, NULL );

  HANDLE handles[] = { quitEvent, GetStdHandle(STD_INPUT_HANDLE) };

  while(true)
  {
    //for more responsiveness from the server, uncomment this
    Sleep(1);

    if(WaitForMultipleObjects(2, handles, FALSE, 0) == (WAIT_OBJECT_0 + 1))
    {
      INPUT_RECORD record;
      DWORD numEvents;
      memset(&record, 0, sizeof(INPUT_RECORD));

      // only quit if this is a key event.  Not a mouse event, etc.
      ReadConsoleInput( handles[1], &record, 1, &numEvents );
      if( record.EventType == KEY_EVENT &&
        record.Event.KeyEvent.bKeyDown &&
        record.Event.KeyEvent.wVirtualKeyCode == 27 )	// 27 == escape key
      {
        break;
      }
    }
    else
    {
      server.process();
    }

  }

  VL_TRACE("Shutting down...");

  server.closeConnections();

  // Dirty hack that lets the server cleanly disconnect everyone
  for (int i = 0; i < 10; ++i)
  {
    server.process();
    wxThread::Sleep(100);
  }

  VL_TRACE("Shutdown complete");

  CloseHandle( quitEvent );
}

//
//void main()
//{
//  {
//    VLStandaloneServer server;
//    server.beginListening(5000);
//    server.closeConnections();
//  }
//
//  {
//    VLStandaloneClient client;
//    client.connectToServer(5000);
//
//    VLPushToTalkController ptt(OIS::KC_LCONTROL);
//    client.getCore().setController(&ptt);
//    client.disconnect();
//  }
//
//  {
//    VLCore server(new VLHeavyServerLogic());
//
//    VLCore client(new VLHeavyClientLogic(), VLFModSoundManager());
//  }
//
//  {
//    VLCore server(new VLEmbeddedServerLogic());
//    VLCore client(new VLEmbeddedClientLogic(), VLFModSoundManager());
//    VLCore p2p(new VLP2PLogic(), VLFModSoundManager());
//  }
//}